# -*- coding: utf-8 -*-

# @File  : supplier_star.py
# @Author: Lomo
# @Site  : lomo.space
# @Date  : 2019-11-27
# @Desc  : 商家评级(大数据/源数据表)

from .db import DB


class SupplierStar(object):
    def __init__(self):
        self.connect = DB.connect()
        self.cur = self.connect.cursor()
        # self.dict_cur = DB().dict_cursor()
        self.table_name = 'supplier_star'

    def get_supplier_by_id(self, supplier_id):
        """
        根据供应商 id 获取其所有原始数据
        :param supplier_id: supplier_id
        :return: tuple, 数据集
        """
        sql = "select * from {0} where supplier_id={1}".format(self.table_name, supplier_id)
        self.cur.execute(sql)
        return self.cur.fetchall()

    def get_suppliers_by_date(self, date):
        """
        获取改段时间供应商总数, 只按照 date 查询是否有重复供应商？
        :param date: score_date
        :return: tuple
        """
        sql = "select * from {0} where score_date='{1}'".format(self.table_name, date)
        self.cur.execute(sql)
        return self.cur.fetchall()

    def get_max_index_by_date(self, index_name, date):
        """
        根据指标名获取某个日期下该指标最大值
        :param index_name: 指标
        :param date: 日期
        :return: Decimal
        """
        sql = "select max({0}) from {1} where score_date='{2}'".format(index_name,
                                                                       self.table_name, date)
        self.cur.execute(sql)
        return self.cur.fetchone()

    def get_min_index_by_date(self, index_name, date):
        """
        根据指标名获取某个日期下该指标最小值
        :param index_name: 指标
        :param date: 日期
        :return: Decimal
        """
        sql = "select min({0}) from {1} where score_date='{2}'".format(index_name,
                                                                       self.table_name, date)
        self.cur.execute(sql)
        return self.cur.fetchone()

    def get_avg_index_by_date(self, index_name, date):
        """
        根据指标名获取某个日期下该指标平均值
        :param index_name: 指标
        :param date: 日期
        :return: Decimal
        """
        sql = "select avg({0}) from {1} where score_date='{2}'".format(index_name,
                                                                       self.table_name, date)
        self.cur.execute(sql)
        return self.cur.fetchone()

    def get_by_index_id_date(self, index_name, supplier_id, date):
        """
        获取某个指标的原始值
        :param index_name: 指标名
        :param supplier_id: 供应商 id
        :param date: 日期
        :return: list
        """
        sql = "select {0} from {1} where supplier_id={2} and score_date='{3}'".format(
            index_name, self.table_name, supplier_id, date
        )
        self.cur.execute(sql)
        return self.cur.fetchone()



